package com.tailyou.hms.firstapp.act;

import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.adapter.PaginationAdapter;
import com.tailyou.hms.firstapp.util.CommonUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PaginationNavigatorActivity extends AppCompatActivity {

    private Button btnM, btnP;
    private final int MAX = 10;
    private final int MIN = 1;
    public static int curPage = 1;
    private RecyclerView rvPagination;
    private List<String> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pagination_navigator);
        btnM = findViewById(R.id.btnM);
        btnP = findViewById(R.id.btnP);
        rvPagination = findViewById(R.id.rvPagination);
        btnM.setOnClickListener(v -> {
            if (curPage == 1) {
                CommonUtils.showToast(PaginationNavigatorActivity.this, "已是第一页");
            } else {
                curPage--;
            }
            renderPagination(curPage);
        });
        btnP.setOnClickListener(v -> {
            if (curPage == MAX) {
                CommonUtils.showToast(PaginationNavigatorActivity.this, "已是最后一页");
            } else {
                curPage++;
            }
            renderPagination(curPage);
        });
        rvPagination.setLayoutManager(new LinearLayoutManager(PaginationNavigatorActivity.this, LinearLayoutManager.HORIZONTAL, false));
        rvPagination.setAdapter(new PaginationAdapter(datas) {

            @Override
            public void clickItem(String text) {
                if (text != "...") {
                    curPage = Integer.valueOf(text);
                    renderPagination(curPage);
                }
            }
        });
        renderPagination(curPage);
    }

    private void renderPagination(int curPage) {
        Set<String> strSet = new HashSet<>();
        // 最小
        strSet.add(String.valueOf(MIN));
        if (curPage == MIN) {
            // 没有前1个，添加后2个
            strSet.add(String.valueOf(curPage + 1));
            strSet.add(String.valueOf(curPage + 2));
            // 如果curPage + 2 < MAX，则添加后...
            if (curPage + 2 < MAX) {
                strSet.add("h");
            }
        } else if (curPage == MAX) {
            // 没有后一个，添加前2个
            // 如果curPage - 2 > MIN，则添加前...
            if (curPage - 2 > MIN) {
                strSet.add("q");
            }
            strSet.add(String.valueOf(curPage - 2));
            strSet.add(String.valueOf(curPage - 1));
        } else {
            if (curPage - 2 > MIN) {
                strSet.add("q");
            }
            // 1前
            strSet.add(String.valueOf(curPage - 1));
            // 当前
            strSet.add(String.valueOf(curPage));
            // 1后
            strSet.add(String.valueOf(curPage + 1));
            if (curPage + 2 < MAX) {
                strSet.add("h");
            }
        }
        // 最大
        strSet.add(String.valueOf(MAX));
        datas.clear();
        datas.addAll(strSet);
        rvPagination.getAdapter().notifyDataSetChanged();
    }

}